<template>
	<view class="uni-padding-wrap" style="margin-top: 20px;">
		<uParse :content="content.details" @preview="preview" @navigate="navigate" />
	</view>
</template>

<script>
	import uParse from '../../components/uParse/src/wxParse.vue'
	export default {
		components: {
			uParse
		},
		data() {
			return {
				cid:"",
				content:"",
				novel:{}
			}
		},
		onLoad(event) {
			// TODO 后面把参数名替换成 payload
			const cid = event.cid || event.payload;
			const novel = event.novel || event.payload;
			// 目前在某些平台参数会被主动 decode，暂时这样处理。
			try {
				this.novel = JSON.parse(decodeURIComponent(novel));
			} catch (error) {
				this.novel = JSON.parse(novel);
			}
			this.cid = cid;
			//题目
			uni.setNavigationBarTitle({
				title: this.novel.title
			});
			//根据小说id获取章节信息
			this.getDetail();
		},
		methods: {
			getDetail(){
				uni.request({
					url: 'http://localhost:8080/content/' + this.cid,
					success: data => {
						if (data.statusCode == 200) {
							this.content = data.data.body.content;
						}
					},
					fail: () => {
						console.log('fail');
					}
				});
			},
			preview(src, e) {
				// do something
				console.log("src: " + src);
			},
			navigate(href, e) {
				// 如允许点击超链接跳转，则应该打开一个新页面，并传入href，由新页面内嵌webview组件负责显示该链接内容
				console.log("href: " + href);
				uni.showModal({
					content : "点击链接为：" + href,
					showCancel:false
				})
			}
		}
	}
</script>

<style>
@import url("../../components/uParse/src/wxParse.css");
</style>
